package zhangh.stu.netttpro.demo.NIO;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author by KoVaVo
 * @Date 2021-01-16 17:37
 */
public class NIOFileChannel {
    public static void main(String[] args) throws Exception {

        String str = " Hello , KoVaVo";
        //创建一个输出流-》channel
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file_channeltest");
        FileChannel fileChannel = fileOutputStream.getChannel();
        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入到byteBuffer中
        byteBuffer.put(str.getBytes());
        //反转
        byteBuffer.flip();
        //将byteBuffer数据写入到fileChannel中
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}
